package com.genokon.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.core.convert.converter.Converter;
import org.springframework.lang.NonNull;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author by baichaoda
 * @date 2020/11/4 9:23
 */
@Slf4j
public class StringToDateConverter implements Converter<String, Date> {
    private static int SHOT_TIME = 10;
    @Override
    public Date convert(@NonNull String source) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat simpleDateFormat24 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date rs = null;
        try {
            if (source.length() == SHOT_TIME){
                rs = simpleDateFormat.parse(source);
            }else {
                rs = simpleDateFormat24.parse(source);
            }
        } catch (ParseException e) {
            log.error("{} Date format error", source);
        }
        return rs;

    }
}
